package mysql

import (
	"gorm.io/gorm"
	work "short-video-media/common/proto"
)

type Works struct {
	gorm.Model
	UserId       int64  `gorm:"type:int(10);" json:"user_id"`
	WorksName    string `gorm:"type:varchar(50);" json:"works_name"`       //作品名称
	WorksContent string `gorm:"type:varchar(100);" json:"works_content"`   //作品描述
	WorksType    int64  `gorm:"type:int(3);" json:"works_type"`            //作品类型 1图片 2视频
	WorksUrl     string `gorm:"type:varchar(255);" json:"works_url"`       //作品链接
	LikeCount    int64  `gorm:"type:int(10);default:0" json:"like_count" ` //点赞数
	TimeCount    string `gorm:"type:varchar(20) " json:"time_count"`       //作品时长
	AudiStatus   int64  `gorm:"type:int(3);default:1" json:"audi_status"`  //审核状态 1待审核 2已通过 3未通过
}

func (w *Works) AddWork(in *work.AddWorkReq) error {
	wo := &Works{
		UserId:       in.UserId,
		WorksName:    in.WorksName,
		WorksContent: in.WorksContent,
		WorksType:    in.WorksType,
		WorksUrl:     in.WorksUrl,
		TimeCount:    in.TimeCount,
	}
	err := DB.Create(&wo).Error
	if err != nil {
		return err
	}
	return nil
}
func (w *Works) UpdateWork(in *work.UpdateWorkReq) error {
	return DB.Where("id=? And user_id=?", in.WorkId, in.UserId).Updates(&Works{
		WorksName:    in.WorksName,
		WorksContent: in.WorksContent,
		WorksType:    in.WorksType,
		WorksUrl:     in.WorksUrl,
		TimeCount:    in.TimeCount,
	}).Error
}
func (w *Works) FindWork(keyWord string) (work []*Works, err error) {
	err = DB.Where("works_name like ?", "%"+keyWord+"%").Find(&work).Error
	if err != nil {
		return nil, err
	}
	return work, nil
}
